import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextArea textShow;
	private JPanel panelOption;

	private JPanel panelNew;
	private JTextField textLength_New;
	private JTextArea textArea_New;

	private JPanel panelGet;
	private JTextField textIndex_Get;
	private JTextField textValue_Get;
	private JLabel labelResult_Insert;

	private JPanel panelInsert;
	private JTextField textIndex_Insert;
	private JTextField textValue_Insert;

	private JPanel panelDelect;
	private JTextField textIndex_Delect;
	private JLabel labelResult_Delect;

	private JPanel panelFind;
	private JTextField textValue_Find;
	private JLabel labelResult_find;

	private JPanel panelShow;
	private JLabel labelMessage_Show;

	private LinearList linearList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Linear list");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 551, 435);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btnNew = new JButton("New");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				switchPanel("New");
			}
		});
		btnNew.setBounds(391, 19, 113, 27);
		contentPane.add(btnNew);

		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnClear_P_call();
			}
		});
		btnClear.setBounds(391, 65, 113, 27);
		contentPane.add(btnClear);

		JButton btnLength = new JButton("Length");
		btnLength.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnLength_P_call();
			}
		});
		btnLength.setBounds(391, 111, 113, 27);
		contentPane.add(btnLength);

		JButton btnIsEmpty = new JButton("isEmpty");
		btnIsEmpty.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnIsEmpty_P_call();
			}
		});
		btnIsEmpty.setBounds(391, 157, 113, 27);
		contentPane.add(btnIsEmpty);

		JButton btnGetValue = new JButton("getValue");
		btnGetValue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (linearList == null) {
					showMessage("Click New to create a linear list!");
					return;
				}
				switchPanel("Get");
			}
		});
		btnGetValue.setBounds(391, 203, 113, 27);
		contentPane.add(btnGetValue);

		JButton btnInsert = new JButton("Insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (linearList == null) {
					showMessage("Click New to create a linear list!");
					return;
				}
				switchPanel("Insert");
			}
		});
		btnInsert.setBounds(391, 249, 113, 27);
		contentPane.add(btnInsert);

		JButton btnDelect = new JButton("Delect");
		btnDelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (linearList == null) {
					showMessage("Click New to create a linear list!");
					return;
				}
				switchPanel("Delect");
			}
		});
		btnDelect.setBounds(391, 295, 113, 27);
		contentPane.add(btnDelect);

		textShow = new JTextArea();
		textShow.setEditable(false);
		textShow.setLineWrap(true);
		textShow.setBounds(14, 44, 363, 140);
		contentPane.add(textShow);
		textShow.setColumns(10);

		JLabel lblNewLabel = new JLabel("Linear list:");
		lblNewLabel.setBounds(14, 25, 138, 18);
		contentPane.add(lblNewLabel);

		JButton btnFind = new JButton("Find");
		btnFind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (linearList == null) {
					showMessage("Click New to create a linear list!");
					return;
				}
				switchPanel("Find");
			}
		});
		btnFind.setBounds(391, 341, 113, 27);
		contentPane.add(btnFind);

		panelOption = new JPanel();
		panelOption.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelOption.setBounds(14, 203, 363, 172);
		contentPane.add(panelOption);
		panelOption.setLayout(null);

		// -------------------------------------
		panelNew = new JPanel();
		panelNew.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelNew.setBounds(0, 0, 363, 172);
		panelNew.setLayout(null);

		textLength_New = new JTextField();
		textLength_New.setBounds(75, 27, 197, 24);
		panelNew.add(textLength_New);
		textLength_New.setColumns(10);

		JLabel lblLength_New = new JLabel("Size:");
		lblLength_New.setBounds(15, 30, 72, 18);
		panelNew.add(lblLength_New);

		textArea_New = new JTextArea();
		textArea_New.setBounds(15, 85, 257, 74);
		panelNew.add(textArea_New);

		JLabel lblValues_New = new JLabel("Values(split by comma):");
		lblValues_New.setBounds(15, 64, 257, 18);
		panelNew.add(lblValues_New);

		JButton btnNew_P = new JButton("New");
		btnNew_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnNew_P_call();
			}
		});
		btnNew_P.setBounds(286, 72, 63, 27);
		panelNew.add(btnNew_P);

		// --------------------------------------
		panelGet = new JPanel();
		panelGet.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelGet.setBounds(0, 0, 363, 172);
		panelGet.setLayout(null);

		JLabel lblIndex_Get = new JLabel("Index:");
		lblIndex_Get.setBounds(14, 45, 56, 18);
		panelGet.add(lblIndex_Get);

		textIndex_Get = new JTextField();
		textIndex_Get.setColumns(10);
		textIndex_Get.setBounds(70, 42, 163, 24);
		panelGet.add(textIndex_Get);

		JButton btnGet_P = new JButton("getValue");
		btnGet_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnGet_P_call();
			}
		});
		btnGet_P.setBounds(247, 41, 102, 27);
		panelGet.add(btnGet_P);

		JLabel lblValue_Get = new JLabel("Value:");
		lblValue_Get.setBounds(14, 108, 56, 18);
		panelGet.add(lblValue_Get);

		textValue_Get = new JTextField();
		textValue_Get.setColumns(10);
		textValue_Get.setBounds(70, 105, 279, 24);
		panelGet.add(textValue_Get);

		// --------------------------------------------------
		panelInsert = new JPanel();
		panelInsert.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelInsert.setLayout(null);
		panelInsert.setBounds(0, 0, 363, 172);

		JLabel labelIndex_Insert = new JLabel("Index:");
		labelIndex_Insert.setBounds(14, 29, 56, 18);
		panelInsert.add(labelIndex_Insert);

		textIndex_Insert = new JTextField();
		textIndex_Insert.setColumns(10);
		textIndex_Insert.setBounds(70, 26, 163, 24);
		panelInsert.add(textIndex_Insert);

		JButton btnInsert_P = new JButton("insert");
		btnInsert_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnInsert_P_call();
			}
		});
		btnInsert_P.setBounds(247, 50, 102, 27);
		panelInsert.add(btnInsert_P);

		JLabel label_1 = new JLabel("Value:");
		label_1.setBounds(14, 76, 56, 18);
		panelInsert.add(label_1);

		textValue_Insert = new JTextField();
		textValue_Insert.setColumns(10);
		textValue_Insert.setBounds(70, 73, 163, 24);
		panelInsert.add(textValue_Insert);

		labelResult_Insert = new JLabel();
		labelResult_Insert.setHorizontalAlignment(SwingConstants.CENTER);
		labelResult_Insert.setBounds(14, 123, 335, 18);
		panelInsert.add(labelResult_Insert);

		// --------------------------------------------
		panelDelect = new JPanel();
		panelDelect.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelDelect.setLayout(null);
		panelDelect.setBounds(0, 0, 363, 172);

		JLabel labelIndex_Delect = new JLabel("Index:");
		labelIndex_Delect.setBounds(14, 45, 56, 18);
		panelDelect.add(labelIndex_Delect);

		textIndex_Delect = new JTextField();
		textIndex_Delect.setColumns(10);
		textIndex_Delect.setBounds(70, 42, 163, 24);
		panelDelect.add(textIndex_Delect);

		JButton btnDelect_P = new JButton("Delect");
		btnDelect_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDelect_P_call();
			}
		});
		btnDelect_P.setBounds(247, 41, 102, 27);
		panelDelect.add(btnDelect_P);

		labelResult_Delect = new JLabel();
		labelResult_Delect.setHorizontalAlignment(SwingConstants.CENTER);
		labelResult_Delect.setBounds(14, 108, 335, 18);
		panelDelect.add(labelResult_Delect);

		// --------------------------------------------------
		panelFind = new JPanel();
		panelFind.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelFind.setLayout(null);
		panelFind.setBounds(0, 0, 363, 172);

		JLabel lblValue_Find = new JLabel("Value:");
		lblValue_Find.setBounds(14, 45, 56, 18);
		panelFind.add(lblValue_Find);

		textValue_Find = new JTextField();
		textValue_Find.setColumns(10);
		textValue_Find.setBounds(70, 42, 163, 24);
		panelFind.add(textValue_Find);

		JButton btnFind_P = new JButton("Find");
		btnFind_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnFind_P_call();
			}
		});
		btnFind_P.setBounds(247, 41, 102, 27);
		panelFind.add(btnFind_P);

		labelResult_find = new JLabel();
		labelResult_find.setHorizontalAlignment(SwingConstants.CENTER);
		labelResult_find.setBounds(14, 108, 335, 18);
		panelFind.add(labelResult_find);

		// -----------------------------------
		panelShow = new JPanel();
		panelShow.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelShow.setLayout(null);
		panelShow.setBounds(0, 0, 363, 172);

		labelMessage_Show = new JLabel("Empty Message!");
		labelMessage_Show.setHorizontalAlignment(SwingConstants.CENTER);
		labelMessage_Show.setBounds(14, 77, 335, 18);
		panelShow.add(labelMessage_Show);

		// -------------------------------------
		showMessage("Click New to Start!");
	}

	/**
	 * 切换功能框
	 * 
	 * @param panelName
	 *            需要切换的功能框名
	 */
	private void switchPanel(String panelName) {
		// 去除功能框内原有的功能框
		panelOption.removeAll();
		// 切换对应的功能框
		switch (panelName) {
		case "New":
			textArea_New.setText("");
			textLength_New.setText("");
			panelOption.add(panelNew);
			break;
		case "Get":
			textIndex_Get.setText("");
			textValue_Get.setText("");
			panelOption.add(panelGet);
			break;
		case "Insert":
			textIndex_Insert.setText("");
			textValue_Insert.setText("");
			panelOption.add(panelInsert);
			break;
		case "Delect":
			textIndex_Delect.setText("");
			panelOption.add(panelDelect);
			break;
		case "Find":
			textValue_Find.setText("");
			panelOption.add(panelFind);
			break;
		case "Show":
			panelOption.add(panelShow);
			break;
		}
		// 通知重绘
		panelOption.repaint();
	}

	/**
	 * 显示一个消息或结果
	 * 
	 * @param message
	 *            需要显示的信息
	 */
	private void showMessage(String message) {
		// 设置显示的信息
		labelMessage_Show.setText(message);
		// 切换到消息功能框
		switchPanel("Show");
	}

	/**
	 * 在输出框显示线性表
	 */
	private void showList() {
		linearList.showLinearList(textShow);
	}

	/**
	 * 新建线性表按钮监听调用方法
	 */
	private void btnNew_P_call() {
		if (textLength_New.getText().length() < 1) {// 判断是否输入了线性表大小参数
			showMessage("Pleas insert a number!");
			return;
		}
		try {
			// 获得线性表大小参数
			int size = Integer.parseInt(textLength_New.getText());
			// 实例化线性表
			linearList = new LinearList(size);
		} catch (Exception e) {// 产生了异常
			showMessage("Error:" + e.getMessage());
			return;
		}
		// 获得线性表初始化数据输入
		String inputStr = textArea_New.getText();
		if (inputStr.length() > 0) {// 输入了线性表初始参数
			try {
				// 初始化线性表初始参数
				linearList.setValues(inputStr);
			} catch (Exception e) {// 产生了异常
				textArea_New.setText("");
				showMessage("Error:" + e.getMessage());
				// 新建线性表失败
				linearList = null;
				return;
			}
		}
		showMessage("Created a linear list!");
		// 展示线性表
		showList();
	}

	/**
	 * 清除按钮监听调用方法
	 */
	private void btnClear_P_call() {
		if (linearList == null) {// 判断是否已经创建了线性表
			showMessage("Click New to create a linear list!");
			return;
		}
		// 清空线性表
		linearList.clear();
		showMessage("Cleared the linear list!");
		// 展示线性表
		showList();
	}

	/**
	 * 获得线性表元素个数按钮监听调用方法
	 */
	private void btnLength_P_call() {
		// 判断是否已经创建了线性表
		if (linearList == null) {
			showMessage("Click New to create a linear list!");
			return;
		}
		// 获得线性表元素个数
		int length = linearList.getLength();
		// 展示线性表元素个数
		showMessage("length = " + length);
	}

	/**
	 * 判断线性表是否为空按钮监听调用方法
	 */
	private void btnIsEmpty_P_call() {
		// 判断当前是否已经创建了线性表
		if (linearList == null) {
			showMessage("Click New to create a linear list!");
			return;
		}
		if (linearList.isEmpty()) {// 线性表为空
			showMessage("The linear is empty!");
		} else {// 线性表不为空
			showMessage("The linear is not empty!");
		}
	}

	/**
	 * 获得线性表元素值的按钮监听调用
	 */
	private void btnGet_P_call() {
		if (textIndex_Get.getText().length() < 1) {// 是否输入了元素的索引
			showMessage("Please input a number!");
		}
		// 元素的值
		int value;
		try {
			// 获得输入的元素索引
			int index = Integer.parseInt(textIndex_Get.getText());
			// 获得对应索引的值
			value = linearList.getValue(index);
		} catch (Exception e) {// 产生了异常
			showMessage("Error:" + e.getMessage());
			return;
		}
		// 展示元素的值
		textValue_Get.setText(value + "");
	}

	/**
	 * 插入元素按钮监听调用方法
	 */
	private void btnInsert_P_call() {
		if (textIndex_Insert.getText().length() < 1) {// 是否输入了插入元素的索引
			showMessage("Please input a number!");
			return;
		}
		if (textValue_Insert.getText().length() < 1) {// 是否输入的插入元素的值
			showMessage("Please input a number!");
			return;
		}
		try {
			// 获得插入元素的索引
			int index = Integer.parseInt(textIndex_Insert.getText());
			// 获得插入元素的值
			int value = Integer.parseInt(textValue_Insert.getText());
			// 插入元素
			linearList.insertValue(index, value);
		} catch (Exception e) {// 产生了异常
			showMessage("Error:" + e.getMessage());
			return;
		}
		labelResult_Insert.setText("Inserted an element!");
		// 展示插入后的线性表
		linearList.showLinearList(textShow);
	}

	/**
	 * 删除元素按钮监听调用
	 */
	private void btnDelect_P_call() {
		if (textIndex_Delect.getText().length() < 1) {// 是否输入了删除元素的索引
			showMessage("Please input a number!");
			return;
		}
		try {
			// 获得删除元素的索引
			int index = Integer.parseInt(textIndex_Delect.getText());
			// 删除对应索引的元素
			linearList.delect(index);
		} catch (Exception e) {// 产生了异常
			showMessage("Error:" + e.getMessage());
			return;
		}
		labelResult_Delect.setText("Delected an element!");
		// 展示删除元素后的线性表
		linearList.showLinearList(textShow);
	}

	/**
	 * 查找元素按钮监听调用方法
	 */
	private void btnFind_P_call() {
		if (textValue_Find.getText().length() < 1) {// 是否输入了需要查找元素的值
			showMessage("Please input a number!");
			return;
		}
		// 查找元素的值
		int value;
		// 查找元素值第一次出现的索引
		int index;
		try {
			// 获得查找元素的值
			value = Integer.parseInt(textValue_Find.getText());
			// 查找元素第一次出现的索引
			index = linearList.find(value);
		} catch (Exception e) {// 产生了异常
			showMessage("Error:" + e.getMessage());
			return;
		}
		if (index < 0) {// 不存在等于该值的元素
			labelResult_find.setText("Can't find " + value + " !");
		} else {// 找到了元素值对应的索引
			labelResult_find.setText("value = " + value + " ; index = " + index);
		}
	}
}
